/*******************************************************************************
 * Copyright (c) 2006 Ultimate Technology, Inc.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     Bojan Vukojevic - initial API and implementation
 *******************************************************************************/
package com.ultimatetech.cim.dialogs;



import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.jface.viewers.ISelectionChangedListener;
import org.eclipse.jface.viewers.IStructuredContentProvider;
import org.eclipse.jface.viewers.ITableLabelProvider;
import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.jface.viewers.SelectionChangedEvent;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.KeyEvent;
import org.eclipse.swt.events.KeyListener;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.Text;

import com.ultimatetech.cim.CIMPlugin;
import com.ultimatetech.cim.views.TreeObject;


public class FindClass extends Dialog {
	private Text findClassName = null;
	private TableViewer viewer = null;
	Object[] elements = null;
	int selectedItemInd = 0;
	public FindClass(Shell parentShell, Object[] elems) {
		super(parentShell);	
		elements = elems;
	}
	
	
	protected Control createDialogArea(Composite parent) {		
		Composite composite = (Composite)super.createDialogArea(parent);
		createSearchFieldAndList(composite);		
		return composite;
	}
	
	protected void createSearchFieldAndList(Composite parent){
		Composite composite = new Composite(parent, SWT.NONE);	
		
		GridLayout layout = new GridLayout();
		layout.numColumns = 1; 
		layout.makeColumnsEqualWidth = false;	        
		layout.marginWidth = convertHorizontalDLUsToPixels(IDialogConstants.HORIZONTAL_MARGIN);
		layout.marginHeight = convertVerticalDLUsToPixels(IDialogConstants.VERTICAL_MARGIN);
		layout.horizontalSpacing = convertHorizontalDLUsToPixels(IDialogConstants.HORIZONTAL_SPACING);
		layout.verticalSpacing = convertVerticalDLUsToPixels(IDialogConstants.VERTICAL_SPACING);
		composite.setLayout(layout);	        
		GridData data = new GridData(GridData.HORIZONTAL_ALIGN_BEGINNING
				| GridData.VERTICAL_ALIGN_CENTER);
		data.minimumWidth = 600;
		data.heightHint = 400;
		composite.setLayoutData(data);
		composite.setFont(parent.getFont());
		
		Label title = new Label(composite, SWT.NONE);
		title.setText("Type the name of the class you are searching for. OK will select it in the tree.");
		
		//GridData gd = new GridData();
		//gd.horizontalAlignment = SWT.LEFT;		
		//title.setLayoutData(gd);
		
		GridData gridData = new GridData();
		gridData.grabExcessHorizontalSpace = true;
		gridData.minimumWidth = 400;
		findClassName = new Text(composite, SWT.SINGLE | SWT.BORDER);		
		findClassName.setLayoutData(gridData);
		findClassName.addKeyListener(new SearchKeyListener());
		
		Composite tableComp = new Composite(composite, SWT.NONE);
		GridData tcgd = new GridData();
		tcgd.grabExcessHorizontalSpace = true;
		tcgd.minimumWidth = 400;
		tableComp.setLayoutData(tcgd);
		
		//add table with all classes
		viewer = new TableViewer(tableComp, 
				SWT.BORDER | SWT.SINGLE | SWT.H_SCROLL | SWT.V_SCROLL);		
		viewer.setContentProvider(new ViewContentProvider());
		viewer.setLabelProvider(new ViewLabelProvider());
		viewer.setInput(elements);		
		viewer.getTable().setSize(400, 300);		
		addTableColumns(viewer.getTable());
		viewer.getTable().setHeaderVisible(false);
		viewer.addSelectionChangedListener(new SelectionListener());
		//viewer.setSorter(new ViewerSorter());
		
		
	}
	
	private void addTableColumns(Table classesTable){
		TableColumn column = new TableColumn(classesTable, SWT.LEFT, 0);				
		column.setText("Class Name");
		column.setWidth(200);		
		classesTable.setLinesVisible(false);
				
	}
	 
	class SelectionListener implements ISelectionChangedListener {

		public void selectionChanged(SelectionChangedEvent event) {
			selectedItemInd = viewer.getTable().getSelectionIndex();
			
		}
		
	}
	class SearchKeyListener implements KeyListener {

		public void keyPressed(KeyEvent e) {
		}
		public void keyReleased(KeyEvent e) {
//			get the value of the field and find the match
			String sfc = findClassName.getText();
			for(int i = 0; i < elements.length; i++){
				TreeObject to = (TreeObject)elements[i];
				if(to.getName().startsWith(sfc)){
					//select it in the table
					viewer.getTable().setSelection(i);
					selectedItemInd = i;
					break;
				}
			}
			
		}
		
	}
	public int getSelectedItemIndex() {
		return selectedItemInd;
	}
	
	
	class ViewContentProvider implements IStructuredContentProvider {
		public void inputChanged(Viewer v, Object oldInput, Object newInput) {
		}
		public void dispose() {
		}
		public Object[] getElements(Object parent) {
			return elements;
			
		}
	}
	class ViewLabelProvider extends LabelProvider implements ITableLabelProvider {
		public String getColumnText(Object obj, int index) {
			String result = "";
			if(obj instanceof TreeObject) {
				TreeObject to = (TreeObject) obj;
			
			switch (index) {
				case 0: 
					result = to.getName();
					break;				
				default :
					break; 	
			}
			}
			return result;	
		}
		public Image getColumnImage(Object obj, int index) {
			if(index == 0){
				return CIMPlugin.getDefault().getImage("icons/class_obj.gif");
			}
			return null;
		}
		
	}
	
}
